package next.view.support;

import java.util.ArrayList;
import java.util.List;

import next.TemplateActivity.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;


public class S_3R4_CustomAdapter extends BaseAdapter implements Filterable {
    private Context context;
    
    public ArrayList<S_3R4_Object> items;
    public ArrayList<S_3R4_Object> filtered;
    private Filter filter;
    private List<S_3R4_Object> listS_3R4_Objects;

    public S_3R4_CustomAdapter(Context context, List<S_3R4_Object> listS_3R4_Object) {
    	 this.items = new ArrayList<S_3R4_Object>(listS_3R4_Object);
         this.filter = getFilter();
    	 this.context = context;
         this.listS_3R4_Objects = listS_3R4_Object;
    }
    
    public void addItems(ArrayList<S_3R4_Object> ae)
    {
    	listS_3R4_Objects=ae;
    }
    
    public void addItem(S_3R4_Object e)
    {
    	listS_3R4_Objects.add(e);
    }

    public int getCount() {
        return listS_3R4_Objects.size();
    }

    
    public Object getItem(int position) {
        return listS_3R4_Objects.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup viewGroup) {
    	S_3R4_Object entry = listS_3R4_Objects.get(position);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.generic_list_row_custom_layout, null);
            convertView.setTag(entry);     
        }
        TextView tvContact = (TextView) convertView.findViewById(R.id.generic_list_custom_layoutTV1);
        tvContact.setText( entry.getChain1());
        
        TextView tvMultilevel = (TextView) convertView.findViewById(R.id.generic_list_custom_layoutTV2);
        tvMultilevel.setText( entry.getChain2());
        
        ImageView tvImg = (ImageView) convertView.findViewById(R.id.generic_list_custom_layoutIV1);
        tvImg.setBackgroundResource(R.drawable.icon);
        convertView.setTag(entry); 
        
        convertView.setId(entry.getA());
      
        return convertView;
    }

   
   // @Override
    public Filter getFilter()
    {
        if(filter == null)
            filter = new S_3R4_ObjectsFilter();
        return filter;
    }

    private class S_3R4_ObjectsFilter extends Filter
    {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            // NOTE: this function is *always* called from a background thread, and
            // not the UI thread.
        	constraint = constraint.toString().toLowerCase();
            FilterResults result = new FilterResults();
            //Toast.makeText(context, constraint+" k", Toast.LENGTH_SHORT).show();
            if(constraint != null && constraint.toString().length() > 0)
            {
                ArrayList<S_3R4_Object> filt=new ArrayList<S_3R4_Object>();
                ArrayList<S_3R4_Object> lItems;
                synchronized (items)
                {
                     	lItems = new ArrayList<S_3R4_Object>(items);
                }
                for(int i = 0; i < lItems.size(); i++)
                {
                	S_3R4_Object m = lItems.get(i);
                    if( m.getChain1().toLowerCase().contains(constraint))
                        filt.add(m);
                }
                result.count = filt.size();
                result.values = filt;
             }
            else
            {
                synchronized(items)
                {
                    result.values = items;
                    result.count = items.size();
                }
             }
            return result;
        }

      
		@SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            // NOTE: this function is *always* called from the UI thread.
        	filtered = (ArrayList<S_3R4_Object>)results.values;
        	 if (filtered!=null){
            listS_3R4_Objects.clear();
		
           
            for(int i = 0;i < filtered.size(); i++){
            	listS_3R4_Objects.add(filtered.get(i));
           
            }
            notifyDataSetChanged();
        	 }else  notifyDataSetInvalidated();
        }
        
      }
    
    @Override
    public boolean areAllItemsEnabled()
    {
        return true;
    }
    
    @Override
    public boolean isEnabled(int arg0) {
        return true;
    }

}
